$(function () {
    sheng = function (obj) {
        // 获取市级的下拉标签
        var shi1 = $("#shi1");
        var old_html = "<option>市</option>";
        shi1.html(old_html);

        var qu1 = $("#qu1");
        var old_html1 = "<option>区</option>";
        qu1.html(old_html1);

        var in_option_xian = obj.options[obj.selectedIndex];
        var aid = in_option_xian.value;
        console.log(aid);
        var params = {
            'aid': aid
        };
        $.ajax({
            url: "/user/select_city",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {

            if (resp.errno == '0') {
                // 获取后台返回的市级城市列表
                var city_info_list = resp.city_info_list;
                // 获取标签内内容
                var html_all = shi1.html();
                // 遍历后台发送的数据列表
                for (var i = 0; i < city_info_list.length; i++) {
                    city = city_info_list[i];
                    // 拼接内容
                    opt_jia = "<option value=\"" + city.aid + "\">" + city.atitle + "</option>";
                    html_all += opt_jia
                }
                // 动态添加到页面
                shi1.html(html_all)

            } else {

                shi1.html(old_html)
            }

        })
    };
    shi = function (obj) {
        var in_option_shi = obj.options[obj.selectedIndex];
        var aid = in_option_shi.value;
        // 获取区级的下拉标签
        var qu1 = $("#qu1");
        var old_html = "<option>区</option>";
        qu1.html(old_html);
        var params = {
            'aid': aid
        };
        $.ajax({
            url: "/user/select_city",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {

            if (resp.errno == '0') {
                var city_info_list = resp.city_info_list;
                var html_all = qu1.html();
                for (var i = 0; i < city_info_list.length; i++) {
                    city = city_info_list[i];
                    opt_jia = "<option>" + city.atitle + "</option>";
                    html_all += opt_jia
                }
                qu1.html(html_all)
            } else {
                qu1.html(old_html)
            }
        })
    };

    $("#sub-sure").click(function () {
        var user_name = $("[name='user_name']").val();
        var like_address_sheng = $("#sheng1 option:selected").html();
        var like_address_shi = $("#shi1 option:selected").html();
        var like_address_qu = $("#qu1 option:selected").html();
        var address = $("[name='address']").val();
        var phone = $("[name='phone']").val();


        var like_address = like_address_sheng + like_address_shi + like_address_qu;

        params = {
            'user_name': user_name,
            'like_address': like_address,
            'address': address,
            'phone': phone
        };
        if (user_name == '') {
            alert('收货人姓名不能为空!');
            return;
        } else if (like_address_sheng == '省') {
            alert('请选择省级地区!');
            return;
        } else if (like_address_shi == '市') {
            alert('请选择市级地区!');
            return;
        } else if (like_address_qu == '区') {
            alert('请选择县级地区!');
            return;
        } else if (address == '') {
            alert('详细地址不能为空!');
            return;
        } else if (phone == '') {
            alert('收货人手机号不能为空!');
            return;
        } else if (phone.length != 11) {
            alert('请输入正确的手机号!');
            return;
        }
        $.ajax({
            url: "/user/add_address",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {
            location.reload()
        })
    });

    rm_addr = function (obj) {
        var addr_id = $(obj).attr('addr-id');
        var params = {
            'addr_id': addr_id
        };
        $.ajax({
            url: '/user/rm_addr',
            type: 'post',
            data: JSON.stringify(params),
            contentType: "application/json"
        }).success(function (resp) {
            if (resp.errno == '0') {
                location.reload()
            } else {
                alert(resp.errmsg);
                location.reload()
            }
        })

    }

});